<template>
  <a-modal :open="open" width="850px" title="血液入库大类" centered :ok-button-props="{ disabled: !isCompleted }" @ok="onOk" @cancel="onCancel">
    <a-form :model="formState" class="form-style-flex" :label-col="{ style: { width: '100px' } }">
      <!-- <a-form-item name="localCode" label="储血号" class="w-1/2" v-bind="validateInfos.localCode">
        <a-input v-model:value="formState.localCode" placeholder="请输入" />
        <a-button type="link" class="w-[20%]" @click="onClickMedical">生成</a-button>
      </a-form-item> -->
      <a-form-item name="expireDate" label="失效日期" class="w-1/2" v-bind="validateInfos.expireDate">
        <a-date-picker v-model:value="formState.expireDate" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
      </a-form-item>
      <a-form-item label="血液来源" class="w-1/2">
        <SelectItem v-model:value="formState.bldSourceCode" type="DT00294" trigger="focus" placeholder="请选择" />
      </a-form-item>
      <a-form-item name="collectDate" label="采血日期" class="w-1/2" v-bind="validateInfos.collectDate">
        <a-date-picker v-model:value="formState.collectDate" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
      </a-form-item>
      <a-form-item label="全血号" class="w-1/2">
        <a-input v-model:value="formState.wholeCode" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="入库者" class="w-1/2">
        <SelectTable v-model:value="formState.inStoreEmpName" type="员工" page-size="999" @select="onSelectEmployee('inStoreEmp', $event)" />
      </a-form-item>
      <a-form-item name="externalCode" label="外来血袋号" class="w-1/2" v-bind="validateInfos.externalCode">
        <a-input v-model:value="formState.externalCode" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="入库时间" class="w-1/2">
        <a-date-picker v-model:value="formState.inStoreTime" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
      </a-form-item>
      <a-form-item name="bldTypeCode" label="血型" class="w-1/2" v-bind="validateInfos.bldTypeCode">
        <SelectItem v-model:value="formState.bldTypeCode" type="DT00006" trigger="focus" placeholder="请选择" />
      </a-form-item>
      <a-form-item label="分离日期" class="w-1/2">
        <a-date-picker v-model:value="formState.separateTime" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
      </a-form-item>
      <a-form-item name="bldTypeRhCode" label="RH" class="w-1/2" v-bind="validateInfos.bldTypeRhCode">
        <SelectItem v-model:value="formState.bldTypeRhCode" type="DT00007" trigger="focus" placeholder="请选择" />
      </a-form-item>
      <a-form-item label="制备者" class="w-1/2">
        <SelectTable v-model:value="formState.preparerEmpName" type="员工" page-size="999" @select="onSelectEmployee('preparerEmp', $event)" />
      </a-form-item>
      <a-form-item name="bldComponentCode" label="血液成分" class="w-1/2" v-bind="validateInfos.bldComponentCode">
        <SelectItem v-model:value="formState.bldComponentCode" type="DT00295" trigger="focus" placeholder="请选择" />
      </a-form-item>
      <a-form-item label="取血者" class="w-1/2">
        <SelectTable v-model:value="formState.donnorEmpName" type="员工" page-size="999" @select="onSelectEmployee('donnorEmp', $event)" />
      </a-form-item>
      <a-form-item name="bldVolume" label="储血量" class="w-1/2" v-bind="validateInfos.bldVolume">
        <a-input v-model:value="formState.bldVolume" class="w-[68%] mr-1" placeholder="请输入" />
        <a-input v-model:value="formState.dw" class="w-[30%]" placeholder="请输入" disabled />
      </a-form-item>
      <!-- <a-form-item label="操作者" class="w-1/2">
        <a-input v-model:value="formState.updateUser" placeholder="请输入" />
      </a-form-item> -->
      <a-form-item label="入库价" class="w-1/2">
        <a-input-number v-model:value="formState.price" class="w-full" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="备注" class="w-full">
        <a-textarea v-model:value="formState.memo" :rows="5" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { useRoute } from 'vue-router'
import { post } from '@/utils/request'

const route = useRoute()

const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const formState = ref({
  // localCode: '',
  externalCode: '',
  bldTypeCode: '',
  bldTypeRhCode: '',
  bldComponentCode: '',
  bldVolume: '',
  expireDate: '',
  collectDate: '',
  dw: 'ml'
})

const rulesState = ref({
  // localCode: [{ required: true, message: '请输入储血号' }],
  externalCode: [{ required: true, message: '请输入外来血袋号' }],
  bldTypeCode: [{ required: true, message: '请选择血袋血型' }],
  bldTypeRhCode: [{ required: true, message: '请选择RH血型' }],
  bldComponentCode: [{ required: true, message: '请选择血液成分' }],
  bldVolume: [{ required: true, message: '请选择储血量' }],
  expireDate: [{ required: true, message: '请选择失效日期' }],
  collectDate: [{ required: true, message: '请选择采血日期' }]
})

const { isCompleted, validateInfos } = useForm(formState, rulesState)

const onSelectEmployee = (key, select) => {
  formState.value[key + 'Name'] = select.empName
  formState.value[key + 'Id'] = select.empId
}

const onOk = async () => {
  const { code, msg } = await post('/bloodm/bldStoreInRecord/doSaveBldStoreDetail', { ...formState.value, lsh: route.query?.lsh })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  emit('update:open', false)
  emit('success')
}

const onCancel = () => {
  emit('update:open', false)
}

watch(
  () => props.open,
  (val) => {
    if (val) {
      formState.value = { ...props.rowData, dw: 'ml' }
    }
  }
)
</script>
